package cn.knightzz.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.knightzz.entity.JsonResult;
import cn.knightzz.entity.SysMessage;
import cn.knightzz.service.SysMessageService;
import cn.knightzz.service.SysUserMessageService;
import cn.knightzz.vo.EventCountVO;
import cn.knightzz.vo.MessageVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import springfox.documentation.spring.web.json.Json;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author knightzz
 * @since 2021-05-07
 */
@Slf4j
@RestController
public class SysMessageController {

    @Resource
    SysMessageService messageService;

    @Resource
    SysUserMessageService userMessageService;

    @PostMapping("/messages")
    public JsonResult<Object> addMessage(@RequestBody MessageVO messageVO) {

        JsonResult<Object> result;
        // 将消息添加到消息表
        SysMessage message = new SysMessage();
        BeanUtil.copyProperties(messageVO, message, true);
        List<Long> userIdList = messageVO.getUserIdList();
        try {
            messageService.addMessage(message);
            // 关联消息与用户
            userMessageService.addUserMessageInfos(message.getId(), userIdList);
        } catch (Exception e) {
            log.error("addMessage error !");
            log.error(e.getMessage());
            return new JsonResult<>("201", "消息发送失败!!!");
        }
        result = new JsonResult<>("200", "消息发送成功");
        return result;
    }

    @GetMapping("/messages/author")
    public JsonResult<HashMap<String,Object>> getMessageListByAuthor(@RequestParam("pageNum") Long pageNum,
                                                               @RequestParam("pageSize") Long pageSize,
                                                               @RequestParam("userId") Long userId,
                                                               @RequestParam("query") String query) {

        IPage<SysMessage> pages = messageService.getAllMessageByAuthorId(pageNum, pageSize, query, userId);
        JsonResult<HashMap<String,Object>> result = null;
        if(ObjectUtil.isNotNull(pages)){
            long total = pages.getTotal();
            List<SysMessage> records = pages.getRecords();
            HashMap<String,Object> data = new HashMap<>();
            data.put("total", total);
            data.put("records", records);
            result = new JsonResult<>("200" , "查询成功!");
            result.setData(data);
            return result;
        }else{
            return new JsonResult<>("201", "查询失败!");
        }
    }

    @PutMapping("/messages")
    public JsonResult<Object> editMessage(@RequestBody MessageVO messageVO) {
        SysMessage message = new SysMessage();
        BeanUtil.copyProperties(messageVO, message, true);

        Boolean status = messageService.updateMessage(message);
        JsonResult<Object> result;
        if (status) {
            result = new JsonResult<>("200", "更新成功!");
        } else {
            result = new JsonResult<>("201", "更新失败!");
        }
        return result;
    }

    @DeleteMapping("/messages/{msgId}")
    public JsonResult<Object> deleteMessage(@PathVariable Long msgId) {
        Boolean status = messageService.deleteMessage(msgId);
        JsonResult<Object> result;
        if (status) {
            result = new JsonResult<>("200", "删除成功!");
        } else {
            result = new JsonResult<>("201", "删除失败!");
        }
        return result;
    }
}

